<!doctype html>
<html>
<head>
<script type="text/javascript" src="lib/blockad.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=320, initial-scale=1, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<title>购物车</title>
<style type="text/css">
*{
	box-sizing:border-box;
    font-family: "Heiti SC","Hiragino Sans GB";
}
a{
	text-decoration: none;
	color: #555;
}
body{
	background-color:#F4F4F4;
	padding:0;
	margin:0;
	color:#444;
	font-size:12px;
}
input[type=checkbox]{
	display:none;
}
.item-container,.tz-container{
	background-color:#FFF;
	margin-bottom:20px;
	box-shadow: 0 1px 0 #E8E8E8;
}
.header-info{
	background-color:#FFF;
	display:table;
	height:40px;
	width:100%;
	border-bottom:1px solid #E8E8E8;
}
.edit{
	width:61px;
	height:100%;
	text-align:center;
	position:relative;
}
.item-div{
	height:112px;
	width:100%;
	display:table;
	border-bottom:1px solid #E8E8E8;
}
.header-info>div{
	display:table-cell;
	vertical-align:middle;
}
.header-tag,.header-title{
	padding:0 10px;
}
.header-title{
	text-align:right;
	height:24px;
}
.header-tag{
	width:46px;
}
.item-div>div{
	display:table-cell;
	height:100%;
	vertical-align:middle;
}
.item-check,.header-check{
	width:36px;
}
.item-image{
	width:90px;
	padding:10px 0;
}
.item-image img{
	max-height:90px;
	max-width:90px;
}
.item-image img,.svg-border{
	vertical-align:middle;
	display:block;
	margin:auto;
}
.svg-border{
	width:19px;
	border:1px solid #B2793D;
	height:19px;
	border-radius:50%;
}
.svg-checkbox{
	transform:translate(-1px,-1px);
	-webkit-transform:translate(-1px,-1px);
	width: 19px;
}
.item-div>.item-info{
	vertical-align:top;
	padding:10px 10px;
}
.item-number{
	display:table;
	width:100%;
	height:25px;
}
.item-price,.item-count{
	display:table-cell;
	vertical-align:middle;
}
.item-price .value-price:before,
.item-price .origin-price>del:before{
	content:'¥ ';
}
.item-price .value-price{
    color:red;
}
.item-price .origin-price{
    color:grey;
}
.item-count{
	color:#777;
	text-align:right;
}
.number{
	display:inline-block;
}
.number:after{
	content:' 件';
}
.item-specifications{
	color:#777;
}
.counter{
	display:none;
	white-space:nowrap;
	width:1%;
}
.counter>div{
	border:1px solid #EEE;
	display:inline-block;
	vertical-align: middle;
	position:relative;
}
.counter-m,.counter-p{
	width:23px;
	height:23px;
}
.counter-n{
	width:50px;
	height:23px;
	line-height:22px;
	margin:0 3px;
	text-align: center;
}
.counter-m span:nth-child(1),.counter-p span:nth-child(1){
	position:absolute;
	top:9px;
	left:2px;
	display:inline-block;
	width:17px;
	height:3px;
	background-color:#AAA;
}
.counter-p span:nth-child(2){
	position:absolute;
	left:9px;
	top:2px;
	display:inline-block;
	width:3px;
	height:17px;
	background-color:#AAA;
}
.item-div .item-delete{
	display:none;
	text-align:center;
	background-color:#F00;
	color:#FFF;
	width:0px;
	max-width:0px;
	overflow:hidden;
	white-space:nowrap;
}
.delete-div{
	width:46px;
	overflow:hidden;
	padding:0 10px;
}
.item-div .show-delete{
	width:1px;
	display:table-cell;
	max-width:46px;
}
.d{
	left:-2px;
	width:1px;
}
.dd{
	width: 1px;
    background-color: #EEE;
    height: 20px;
}
.tz-number{
	display:table;
	width:100%;
	height:24px;
}
.tz-number>div{
	display:table-cell;
	vertical-align:middle;
}
.tz-number .counter{
	display:none;
	text-align:right;
	padding:0 10px;
}
.tz-number>.number{
	padding:0 10px;
	text-align:right;
}
.tz-number>.number:after{
	content:" 套";
}
.tz-tag{
	width:80px;
	padding:0 10px;
}
.tz-container .header-title{
    text-align:left;
    line-height:20px;
}
.tz-container .edit{
    text-align:left;
    padding:0 10px;
    width:70px;
}
.tz-edit{
    position:relative;
}
.tz-edit>div{
    position:relative;
    display:inline-block;
    vertical-align:middle;
}
.bottom-bar{
    font-size:14px;
    border-top:1px solid #E8E8E8;
    position:fixed;
    bottom:0;
    display:table;
    width:100%;
    height:44px;
    background-color:#FFF;
}
.bottom-bar>div{
    height:100%;
    display:table-cell;
    vertical-align:middle;
}
.all-check{
    width:36px;
}
.total-price{
    padding:0 10px;
    color:#F00;
    text-align:right;
}
.total-number:before{
    content:"¥";
}
.total-price>div{
    display:inline-block;
}
.submit{
    padding:0 10px;
}
.submit>button{
	border-radius: 8px;
    border:1px solid #B2793D;
    background-color:#B2793D;
    color:#FFF;
    min-width:110px;
    height:80%;
    float:right;
    font-size:16px;
}
.empty-cart{
    width: 100%;
    font-size: 16px;
    margin-top: 30px;
    text-align: center;
    color: #AAA;
}
.submit .disabled{
    background-color: #AAA;
    border-color: #AAA;
}
.warn{
    text-align: center;
    font-size: 20px;
    margin-top: 30px;
}
.warn a{
    color:#666;
    text-decoration: none;
}
.hidden{
	display: none;
}
</style>
</head>
<?php
require_once("config/app_webview_config.php");
require_once("app-component.php");
$user_id=get_magic_quotes_gpc()?$_GET["user_id"]:addslashes($_GET["user_id"]);
$user_token=get_magic_quotes_gpc()?$_GET["user_token"]:addslashes($_GET["user_token"]);
if(!verify($user_id,$user_token))
{
	echo "<div class='warn'><a href='webView://login'>请登录之后再购买</a></div>";
	return;
}
$user_type = getUserType($user_id);
$user_discount = getUserDiscount($user_type['user_type'], $user_type['user_type_class']);
$cart_arr=getShoppingCartAll($user_id);
?>
<body>
<?php
    if (empty($cart_arr)) {
        echo "<div class='empty-cart'>您的购物车是空的</div>";
    }
    $sign=0;
    foreach ($cart_arr as $cart_index => $cart_content)
    {
        if ($cart_content["info"]["cart_type"]==0)
        {
            //处理单品
            if ($sign==0) {
                echo '
                <div class="item-container container" data-cart_type="0">
                    <div class="header-info">
                        <div class="header-check">
                            <div class="svg-border item-check-all" data-checked="true"><img class="svg-checkbox" src="images/check-icon.png"><input type="checkbox" checked></div>
                        </div>
                        <div class="header-tag">全选</div>
                        <div class="header-title">单品</div>
                        <div class="d"><div class="dd"></div></div>
                        <div class="edit edit-count">编辑数量</div>
                        <div class="d"><div class="dd"></div></div>
                        <div class="edit edit-delete">删除单品</div>
                    </div>
                ';
            }
            foreach ($cart_content["items"] as $item_index => $value) {
                $origin_price = '';
                $value_price = '';
                if (intval($value["price_base"]) > 0){
                    $origin_price = round($value["price_base"] + $value["_price_custom"], 2);
                    if (in_array($user_type['user_type'], [3,4,5]) && isset($user_discount[SALES_TYPE_ITEM]['discount'])){
                        $value_price=round(($value["price_base"]+$value["_price_custom"]) * $user_discount[SALES_TYPE_ITEM]['discount'], 2);
                    }else{
                        $value_price=round($value["price_base"] * $value["price_discount"] + $value["_price_custom"] * $value["_price_discount"], 2);
                    }
                    if ($origin_price == $value_price){
                        $origin_price = '';
                    }
                    if ($origin_price > 0 || $origin_price != ''){
                        $origin_price = '<del>'.$origin_price.'</del>';
                    }
                }else{
                    $value_price="请询价";
                }
                
                echo '
                    <div class="item-div'.($value['app_return']!=1?' offline':'').'" data-cartid="'.$cart_content["info"]["cart_id"].'" data-brandid="'.$value["brand_id"].'" data-classid="'.$value["class_id"].'" data-itemid="'.$value["item_id"].'" data-priceid="'.(isset($value["price_id"])?$value["price_id"]:'').'">
                        <div class="item-check">
                            <div class="svg-border item-check-i" data-checked="true"><img class="svg-checkbox" src="images/check-icon.png"><input type="checkbox" checked></div>
                        </div>
                        <div class="item-image">
                            '.($value['app_return']==0?'<a><img src="'.$url.$path.$value["img_url"].'"></a>':'<a href="webView://item_info?id='.$value['item_id'].'"><img src="'.$url.$path.$value["img_url"].'"></a>').'
                        </div>
                        <div class="item-info">
                            <div class="item-title"><a href="webView://item_info?id='.$value['item_id'].'">'.$value["name"].'</a></div>
                            <div class="item-number">
                                <div class="item-price">
                                    <span class="value-price">'.$value_price.'</span>&nbsp;&nbsp;&nbsp;
                                    <span class="origin-price">'.$origin_price.'</span>
                                </div>
                                <div class="item-count">
                                    <div class="number">'.$value["count"].'</div>
                                    <div class="counter">
                                        <div class="counter-m"><span></span></div>
                                        <div class="counter-n">'.$value["count"].'</div>
                                        <div class="counter-p"><span></span><span></span></div>
                                    </div>
                                </div>
                            </div>
                            <div class="item-specifications" data-sku_id=\''.(json_encode($value["sku_id"],JSON_UNESCAPED_UNICODE)).'\'>
                                '.(isset($value["sku"])?parseSku($value["sku"]):"").'
                            </div>
                        </div>
                        <div class="item-delete">
                            <div class="delete-div">删除</div>
                        </div>
                    </div>
                ';
            }
            $sign=1;
        }
    }
    if ($sign==1) {
        echo '</div>';
    }

    foreach ($cart_arr as $cart_index => $cart_content)
    {
        if ($cart_content["info"]["cart_type"]==1)
        {
            //处理套装
            echo '
                <div class="tz-container container" data-id="'.$cart_content["info"]["id"].'" data-cartid="'.$cart_content["info"]["cart_id"].'" data-cart_type="1" data-discount="'.$cart_content["info"]["discount"].'" >
                    <div class="header-info">
                        <div class="header-check">
                            <div class="svg-border tz-check-i" data-checked="true">
                            <img class="svg-checkbox" src="images/check-icon.png">
                            <input type="checkbox" checked></div>
                        </div>
                        <div class="item-image">
                            <img src="'.$url.$path.$cart_content["info"]["img_url"].'">
                        </div>
                        <div class="header-content">
                            <div class="header-title">'.$cart_content["info"]["name"].'</div>
                            <div class="tz-number">
                                <div class="tz-tag">套装套数:</div>
                                <div class="number">'.$cart_content["info"]["count"].'</div>
                                <div class="counter">
                                    <div class="counter-m"><span></span></div>
                                    <div class="counter-n">'.$cart_content["info"]["count"].'</div>
                                    <div class="counter-p"><span></span><span></span></div>
                                </div>
                            </div>
                            <div class="tz-edit">
                                <div class="edit edit-count">编辑数量</div>
                                <div class="d"><div class="dd"></div></div>
                                <div class="edit edit-delete">删除套装</div>
                            </div>
                        </div>
                    </div>';
            foreach ($cart_content["items"] as $item_index => $value)
            {
                $value_price = '';
                $origin_price = '';
                if (intval($value["price_base"])){
                    $origin_price = round($value["price_base"] + $value["_price_custom"],2);
                    if (in_array($user_type['user_type'], [3,4,5]) && isset($user_discount[SALES_TYPE_TZ]['discount'])){
                        $value_price=round(($value["price_base"]+$value["_price_custom"]) * $user_discount[SALES_TYPE_TZ]['discount'], 2);
                    }else{
                        $value_price=round($value["price_base"] * $value["price_discount"] + $value["_price_custom"] * $value["_price_discount"], 2);
                    }
                    if ($origin_price == $value_price){
                        $origin_price = '';
                    }
                    if ($origin_price > 0 || $origin_price != ''){
                        $origin_price = '<del>'.$origin_price.'</del>';
                    }
                }else{
                    $value_price = "请询价";
                }
                //根据后台传过来的数据exclude字段 判断单品是否是 赠品
                isset($cart_content["info"]["exclude"])&&in_array($value["item_id"], $cart_content["info"]["exclude"])?$isExclude=true:$isExclude=false;
                echo '
                <div class="item-div'.($isExclude?" exclude":"").($value['app_return']==0?' offline':'').'" data-itemid="'.$value["item_id"].'" data-priceid="'.$value["price_id"].'">
                    <div class="item-check">
                    </div>
                    <div class="item-image">
                        '.($value['app_return']!=1?'<a><img src="'.$url.$path.$value["img_url"].'"></a>':'<a href="webView://item_info?id='.$value['item_id'].'"><img src="'.$url.$path.$value["img_url"].'"></a>').'
                    </div>
                    <div class="item-info">
                        <div class="item-title">'.$value["name"].'</div>
                        <div class="item-specifications" data-sku_id=\''.(json_encode($value["sku_id"],JSON_UNESCAPED_UNICODE)).'\'>
                            '.(isset($value["sku"])?parseSku($value["sku"]):"").'
                        </div>
                        <div class="item-number">
                            '.($isExclude?"<div style='display:table;'><del style='color:#999;line-height:5px;display:table-cell;'><div class='item-price' style='color:#999;'><span class='value-price'>".$origin_price."</span></div></del><span style='display:table-cell;'>&nbsp;赠品</span></div>":"<div class='item-price'><span class='value-price'>".$value_price."</span>&nbsp;&nbsp;&nbsp;<span class='origin-price'>".$origin_price."</span></div>").'
                            <div class="item-count">
                                <div class="number">'.$value["count"].'</div>
                            </div>
                        </div>
                    </div>
                </div>';
            }
            echo '</div>';
        }
    }
?>

<div style="width:100%;height:40px;">
</div>

<div class="bottom-bar">
	<div class="all-check">
    	<div class="svg-border check-all" data-checked="true"><img class="svg-checkbox" src="images/check-icon.png">
        <input type="checkbox" checked></div>
    </div>
    <div>全选</div>
    <div class="total-price">
    	<div class="total-tag">总计:</div>
        <div class="total-number" id="total-number"></div>
    </div>
    <div class="submit">
    	<button onClick="submitCart()">去结算(<span id="total-count"></span>)</button>
    </div>
</div>
<div class="hidden"></div>
<script type="text/javascript">
bindCheckbox();
bindEdit();
bindCounter();
updatePrice();
bindDelete();
updateAllCheckbox();
setOffItems();
//offline 已经失效的商品
function setOffItems()
{
	var containers=document.querySelectorAll('.offline');
	for(var i=0;i<containers.length;i++)
	{
		var pNode=containers[i].querySelector('.item-image a');
		pNode.style.position='relative';
		var oimg=pNode.querySelector('img');
		oimg.style.position='absolute';
		var img=document.createElement('img');
		img.style.opacity='0.8';
		pNode.appendChild(img);
		img.style.width='90px';
		img.style.height='90px';
		img.src="<?= $url?>furniture/images/efficacy.png";
	}
}
function bindCheckbox()
{
	var i;
	var itemCheckAll=document.querySelectorAll(".item-check-all");
	for(i=0;i<itemCheckAll.length;i++)
	{
		itemCheckAll.item(i).addEventListener("click",function(){
			if(this.querySelector("input[type=checkbox]").checked)
			{
				var flag=false;
				var display="none";
			}
			else
			{
				var flag=true;
				var display="block";
			}
			changeSelf.call(this);
			var items=this.parentNode.parentNode.parentNode.querySelectorAll(".item-check-i");
			for(var j=0;j<items.length;j++)
			{
				items.item(j).dataset.checked=flag;
				items.item(j).querySelector(".svg-checkbox").style.display=display;
				items.item(j).querySelector("input[type=checkbox]").checked=flag;
			}
			updateAllCheckbox();
			updatePrice();
		});
	}
	
	var itemCheckI=document.querySelectorAll(".item-check-i");
	for(i=0;i<itemCheckI.length;i++)
	{
		itemCheckI.item(i).addEventListener("click",function(){
			changeSelf.call(this);
			updateAllCheckbox();
			updatePrice();
		});
	}
	
	var tzCheckI=document.querySelectorAll(".tz-check-i");
	for(i=0;i<tzCheckI.length;i++)
	{
		tzCheckI.item(i).addEventListener("click",function(){
			changeSelf.call(this);
			updateAllCheckbox();
			updatePrice();
		});
	}
	document.querySelector(".check-all").addEventListener("click",function(){
		var allCheck=document.querySelectorAll(".svg-border");
		if(this.querySelector("input[type=checkbox]").checked)
		{
			var flag=false;
			var display="none";
		}
		else
		{
			var flag=true;
			var display="block";
		}
		for(var i=0;i<allCheck.length;i++)
		{
			allCheck.item(i).dataset.checked=flag;
			allCheck.item(i).querySelector(".svg-checkbox").style.display=display;
			allCheck.item(i).querySelector("input[type=checkbox]").checked=flag;
		}
		updateAllCheckbox();
		updatePrice();
	});
}
function changeSelf()
{
	if(this.querySelector("input[type=checkbox]").checked)
	{
		var flag=false;
		var display="none";
	}
	else
	{
		var flag=true;
		var display="block";
	}
	this.dataset.checked=flag;
	this.querySelector(".svg-checkbox").style.display=display;
	this.querySelector("input[type=checkbox]").checked=flag;
}
function updateAllCheckbox()
{
	var i,itemAll=true,tzAll=true;
	var items=document.querySelectorAll(".item-check-i");
	for(i=0;i<items.length;i++)
	{
		if((itemAll=items.item(i).querySelector("input[type=checkbox]").checked)==false)
		{
			break;
		}
	}
	var itemCheckAll=document.querySelector(".item-check-all");
	if (itemCheckAll) {
        if(itemAll)
        {
            itemCheckAll.dataset.checked=true;
            itemCheckAll.querySelector(".svg-checkbox").style.display="block";
            itemCheckAll.querySelector("input[type=checkbox]").checked=true;
        }
        else
        {
            itemCheckAll.dataset.checked=false;
            itemCheckAll.querySelector(".svg-checkbox").style.display="none";
            itemCheckAll.querySelector("input[type=checkbox]").checked=false;
        }
    }
	
	var tzCheckI=document.querySelectorAll(".tz-check-i");
	for(i=0;i<tzCheckI.length;i++)
	{
		if((tzAll=tzCheckI.item(i).querySelector("input[type=checkbox]").checked)==false)
		{
			break;
		}
	}
	var allCheckAll=document.querySelector(".check-all");
	if(itemAll&&tzAll)
	{
		allCheckAll.dataset.checked=true;
		allCheckAll.querySelector(".svg-checkbox").style.display="block";
		allCheckAll.querySelector("input[type=checkbox]").checked=true;
	}
	else
	{
		allCheckAll.dataset.checked=false;
		allCheckAll.querySelector(".svg-checkbox").style.display="none";
		allCheckAll.querySelector("input[type=checkbox]").checked=false;
	}
    var t=document.querySelectorAll(".item-check-i,.tz-check-i");
    for(var i = 0; i < t.length; i++){
        if (t[i].dataset.checked=="true") {
            break;
        }
    }
    var btn=document.querySelector(".submit>button");
    if (i==t.length) {
        btn.classList.remove("enabled");
        btn.classList.add("disabled");
    }
    else if(btn.classList.contains("disabled"))
    {
        btn.classList.remove("disabled");
        btn.classList.add("enabled");
    }
}
var sum_price;
function updatePrice()
{
    sum_price = 0;
	var i,j,total_count=0,tz_count,tz_price,t;
	//单品所有价格
	var items=document.querySelectorAll(".item-div:not(.offline) .item-check-i");
	for(i=0;i<items.length;i++)
	{
		if(items.item(i).querySelector("input[type=checkbox]").checked)
		{
			var item_div=items.item(i).parentNode.parentNode;
            t = parseFloat(item_div.querySelector(".item-price .value-price").textContent);
            t = t ? t : 0;
			sum_price = sum_price + t * parseInt(item_div.querySelector(".number").textContent);
			total_count = total_count + parseInt(item_div.querySelector(".number").textContent);
		}
	}
	//套装所有价格
	var tzs=document.querySelectorAll(".tz-check-i");
	for(i=0;i<tzs.length;i++)
	{
		tz_price = 0;
		tz_count = 0;
		if(tzs.item(i).querySelector("input[type=checkbox]").checked)
		{
			var tz=tzs.item(i).parentNode.parentNode.parentNode;
			var tz_items=tz.querySelectorAll(".item-div:not(.exclude):not(.offline)");
			for(j=0;j<tz_items.length;j++)
			{
                t = parseFloat(tz_items.item(j).querySelector(".item-price .value-price").textContent);
                t = t ? t : 0;
				tz_price = tz_price + t * parseInt(tz_items.item(j).querySelector(".number").textContent);
			}
			sum_price = sum_price + tz_price * parseInt(tz.querySelector(".tz-number .number").textContent);

			var tz_items2 = tz.querySelectorAll(".item-div");
			for(k=0;k<tz_items2.length;k++)
			{
				tz_count = tz_count + parseInt(tz_items2.item(k).querySelector(".item-info .number").textContent);
			}
			total_count = total_count+tz_count * parseInt(tz.querySelector(".tz-number .number").textContent);
		}
	}
	//套装所有数量
	document.getElementById("total-number").textContent = sum_price?parseInt(sum_price):"请询价";
	document.getElementById("total-count").textContent = total_count;
}
function bindEdit()
{
    if (document.querySelector(".item-container")) {
        var item_edit=document.querySelector(".item-container .edit-count");
        var i;
		item_edit.addEventListener("click",function(){
			var j;
			var counters=this.parentNode.parentNode.querySelectorAll(".counter");
			if(this.textContent=="编辑数量")
			{
				for(j=0;j<counters.length;j++)
				{
					counters.item(j).parentNode.querySelector(".number").style.display="none";
					counters.item(j).style.display="table-cell";
				}
				this.textContent="完成";
			}
			else if(this.textContent=="完成")
			{
				for(j=0;j<counters.length;j++)
				{
					counters.item(j).parentNode.querySelector(".number").style.display="inline-block";
					counters.item(j).style.display="none";
				}
				this.textContent="编辑数量";
			}
		});
		document.querySelector(".item-container .edit-delete").addEventListener("click",function(){
			var i;
			var items=document.querySelectorAll(".item-container .item-delete");
			if(this.textContent=="删除单品")
			{
				for(i=0;i<items.length;i++)
				{
					items.item(i).classList.add("show-delete");
				}
				this.textContent="完成";
			}
			else if(this.textContent=="完成")
			{
				for(i=0;i<items.length;i++)
				{
					items.item(i).classList.remove("show-delete");
				}
				this.textContent="删除单品";
			}
		});
    }
	if(document.querySelectorAll(".tz-container .edit-count")){
        var tz_edits=document.querySelectorAll(".tz-container .edit-count");
		for(i=0;i<tz_edits.length;i++)
		{
			tz_edits.item(i).addEventListener("click",function(){
				var tzNumber=this.parentNode.parentNode.querySelector(".tz-number");
				if(this.textContent=="编辑数量")
				{
					tzNumber.querySelector(".counter").style.display="table-cell";
					tzNumber.querySelector(".number").style.display="none";
					this.textContent="完成";
				}
				else if(this.textContent=="完成")
				{
					tzNumber.querySelector(".counter").style.display="none";
					tzNumber.querySelector(".number").style.display="table-cell";
					this.textContent="编辑数量";
				}
			});
		}
    }
}
function bindCounter()
{
	var counterM=document.getElementsByClassName("counter-m");
	var counterP=document.getElementsByClassName("counter-p");
	var i;
	for(i=0;i<counterM.length;i++)
	{
		counterM.item(i).addEventListener("click",calc);
		counterP.item(i).addEventListener("click",calc);
	}
	function calc()
	{
		var counter=this.parentNode;
		var number=parseInt(counter.querySelector(".counter-n").textContent);
		if(this.classList.contains("counter-m"))
		{
			if(number>1)
			{
				counter.querySelector(".counter-n").textContent=number-1;
			}
		}
		else if(this.classList.contains("counter-p"))
		{
			if(number<10000)
			{
				counter.querySelector(".counter-n").textContent=number+1;
			}
		}
		counter.parentNode.querySelector(".number").textContent=counter.querySelector(".counter-n").textContent;
		updatePrice();
	}
}
function bindDelete()
{
	var i,items=document.querySelectorAll(".item-container .item-delete");
	for(i=0;i<items.length;i++)
	{
		items.item(i).addEventListener("click",function(){
			var itemDiv=this.parentNode;
			var container=itemDiv.parentNode;
			var fd=new FormData();
			fd.append("q","deleteShoppingCart");
			fd.append("cart_id",itemDiv.dataset.cartid);
			fd.append("user_id",<?= $user_id?>);
			fd.append("user_token","<?= $user_token?>");
			var xml=new XMLHttpRequest();
			xml.open("POST","calc.php",false);
			xml.onreadystatechange=function(){
				if(xml.readyState===4&&xml.status===200)
				{
					if(xml.responseText==1)
					{
						container.removeChild(itemDiv);
						updateAllCheckbox();
						updatePrice();
					}
					else
					{
						alert("删除失败请重试");
					}
				}
			};
			xml.send(fd);
		});
	}
	
	var tzs=document.querySelectorAll(".tz-container .edit-delete");
	for(i=0;i<tzs.length;i++)
	{
		tzs.item(i).addEventListener("click",function(){
			var tz_div=this.parentNode.parentNode.parentNode.parentNode;
			var fd=new FormData();
			fd.append("q","deleteShoppingCart");
			fd.append("cart_id",tz_div.dataset.cartid);
            fd.append("user_id",<?= $user_id?>);
            fd.append("user_token","<?= $user_token?>");
			var xml=new XMLHttpRequest();
			xml.open("POST","calc.php",false);
			xml.onreadystatechange=function(){
				if(xml.readyState===4&&xml.status===200)
				{
					if(xml.responseText==1)
					{
						tz_div.parentNode.removeChild(tz_div);
						updateAllCheckbox();
						updatePrice();
					}
					else
					{
						alert("删除失败请重试");
					}
				}
			};
			xml.send(fd);
		});
	}
}
function submitCart()
{

	//验证单品数不可以超过50个
	var total_count_valid=parseInt(document.getElementById("total-count").textContent);
	if(total_count_valid>50){
		alert("单品不可以超过50个，请重新编辑购物车!");
		return;
		}
    //订单金额不能超过1千万
    if (sum_price > 10000000) {
        alert('每个订单总金额不能超过1千万');
        return;
    }

    if (document.querySelector(".submit button").classList.contains("disabled")) {return;}
	var obj={},j,n=0,m=0,e;
	//如果有单品，生成单品obj
	var items=document.querySelectorAll(".item-container .item-div");
    if (items.length>0)
    {
        for(var i = 0; i < items.length; i++)
        {
            if(items.item(i).querySelector("input[type=checkbox]").checked && !items.item(i).classList.contains('offline'))
            {
            	if (!obj.hasOwnProperty('singleItem')) {
            		obj["singleItem"]=[];
            	}
                obj["singleItem"][n]={};
                obj["singleItem"][n]["info"]={};
                obj["singleItem"][n]["info"]["cart_id"]=items.item(i).dataset.cartid;
                obj["singleItem"][n]["info"]["cart_type"]=0;
                obj["singleItem"][n]["items"]=[];
                obj["singleItem"][n]["items"][0]={};
                obj["singleItem"][n]["items"][0]["item_id"]=items.item(i).dataset.itemid;
                obj["singleItem"][n]["items"][0]["brand_id"]=items.item(i).dataset.brandid;
                obj["singleItem"][n]["items"][0]["class_id"]=items.item(i).dataset.classid;
                obj["singleItem"][n]["items"][0]["price_id"]=items.item(i).dataset.priceid;
                obj["singleItem"][n]["items"][0]["count"]=items.item(i).querySelector(".number").textContent;
                obj["singleItem"][n]["items"][0]["sku_id"]=JSON.parse(items.item(i).querySelector(".item-specifications").dataset.sku_id);
                n++;
            }
        }
    }
	
	//如果有套装，生成套装obj
	var tzs=document.querySelectorAll(".tz-container");
    if(tzs.length>0)
    {
    	n=0;
        for(i=0;i<tzs.length;i++)
        {
        	e=0;m=0;
            if(tzs.item(i).querySelector("input[type=checkbox]").checked)
            {
            	if (!obj.hasOwnProperty('tz')) {
            		obj["tz"]=[];
            	}
                obj["tz"][n]={};
                obj["tz"][n]["info"]={};
                obj["tz"][n]["items"]=[];
                obj["tz"][n]["info"]['id']=tzs.item(i).dataset.id;
                obj["tz"][n]["info"]['cart_type']=tzs.item(i).dataset.cart_type;
                obj["tz"][n]["info"]["cart_id"]=tzs.item(i).dataset.cartid;
                obj["tz"][n]["info"]["discount"]=tzs.item(i).dataset.discount;
                obj["tz"][n]["info"]["count"]=parseInt(tzs.item(i).querySelector(".tz-number .number").textContent);
                var tz_items=tzs.item(i).querySelectorAll(".item-div");
                for(j=0;j<tz_items.length;j++)
                {
					if (tz_items.item(j).classList.contains("offline")) {
						continue;
					}
                    obj["tz"][n]["items"][m]={};
                    obj["tz"][n]["items"][m]["item_id"]=tz_items.item(j).dataset.itemid;

                    if (tz_items.item(j).classList.contains("exclude")) {
                    	if(obj["tz"][n]["info"].hasOwnProperty("exclude")){
                    		obj["tz"][n]["info"]["exclude"][e++]=obj["tz"][n]["items"][m]["item_id"];
                    	}else{
                    		obj["tz"][n]["info"]["exclude"]=[];
                    		obj["tz"][n]["info"]["exclude"][e++]=obj["tz"][n]["items"][m]["item_id"];
                    	}
                    }
                    
                    obj["tz"][n]["items"][m]["price_id"]=tz_items.item(j).dataset.priceid;
                    obj["tz"][n]["items"][m]["count"]=parseInt(tz_items.item(j).querySelector(".number").textContent);
                    obj["tz"][n]["items"][m]["sku_id"]=JSON.parse(tz_items.item(j).querySelector(".item-specifications").dataset.sku_id);
                    m++;
                }
                n++;
            }
        }
    }
	if(JSON.stringify(obj)=='{}'){
		document.querySelector('.total-number').textContent='无单品可提交';
		return false;
	}else{
		if(Object.prototype.toString.call(obj['tz'])=='[object Array]'){
			var t=false;
			for(var i=0; i<obj['tz'].length; i++)
			{
				if(obj.tz[i].items.length>0)
				{
					t=true;
				}
			}
			if(!t){
				document.querySelector('.total-number').textContent='无单品可提交';
				return false;
			}
		}
	}
	var form=document.createElement("form");
    form.action="order.php";
    form.method="post";
    
    var t1=document.createElement("textarea");
    t1.name="obj";
    t1.value=JSON.stringify(obj);
    var t3=document.createElement("input");
    t3.name="user_id";
    t3.value="<?= $user_id?>";
    var t4=document.createElement("textarea");
    t4.name="user_token";
    t4.value="<?= $user_token?>";
    
    form.appendChild(t1);
    form.appendChild(t3);
    form.appendChild(t4);
    document.querySelector('.hidden').appendChild(form);
    form.submit();
}
</script>
</body>
</html>